package com.dou.sample.opengl_project;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

import com.dou.sample.opengl_project.data.Mallet;
import com.dou.sample.opengl_project.data.Puck;
import com.dou.sample.opengl_project.data.Table;
import com.dou.sample.opengl_project.shader.ColorShaderProgram;
import com.dou.sample.opengl_project.shader.TextureShaderProgram;
import com.dou.sample.opengl_project.utls.GeoMetry;
import com.dou.sample.opengl_project.utls.MatrixHelper;
import com.dou.sample.opengl_project.utls.TextureHelper;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.rotateM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.setLookAtM;
import static android.opengl.Matrix.translateM;

/**
 * Created by Dou on 2018/1/16.
 */

public class TextureRenderer implements Renderer {

    private static final String TAG = TextureRenderer.class.getSimpleName();

    Context context;

    Table table;
    Mallet mallet;
    Puck puck;

    int texture;

    float[] viewMatrix = new float[16];
    float[] viewProjectionMatrix = new float[16];
    float[] modelViewProjectionMatrix = new float[16];

    float[] projectionMatrix = new float[16];
    float[] modelMatrix = new float[16];

    ColorShaderProgram colorShaderProgram;
    TextureShaderProgram textureShaderProgram;

    boolean malletPressed = false;
    GeoMetry.Point blueMalletPosition;

    public TextureRenderer(Context context){
        this.context = context;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0, 0, 0, 0);

        // 加载着色器数据
        table = new Table();
        mallet = new Mallet(0.08f, 0.15f, 32);
        puck = new Puck(0.06f, 0.02f, 32);

        // 创建着色器
        textureShaderProgram = new TextureShaderProgram(context);
        colorShaderProgram = new ColorShaderProgram(context);

        texture = TextureHelper.loadTexture(context, R.drawable.air_hockey_surface);

        blueMalletPosition = new GeoMetry.Point(0f, mallet.height / 2f, 0.4f);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);

        // 创建透视矩阵
        MatrixHelper.perspectiveM(projectionMatrix, 60, (float) width / (float) height, 1f, 10f);

        setLookAtM(viewMatrix, 0, 0, 1.2f, 2.2f, 0,0,0,0,1,0);

//        // 创建位移矩阵和旋转矩阵
//        Matrix.setIdentityM(modelMatrix, 0);
//        Matrix.translateM(modelMatrix, 0, 0, 0, -2.5f);
//        Matrix.rotateM(modelMatrix, 0, -60, 1, 0, 0);
//
//        // 矩阵乘得到一个新矩阵
//        float[] temp = new float[16];
//        Matrix.multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
//        System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        multiplyMM(viewProjectionMatrix, 0, projectionMatrix,0, viewMatrix, 0);

        // 着色器着色
        positionTableInScene();
        textureShaderProgram.useProgram();
        textureShaderProgram.setUniform(modelViewProjectionMatrix, texture);
        table.bindData(textureShaderProgram);
        table.draw();

        // draw mallet 1
        positionObjectInScene(0, mallet.height / 2f, -0.4f);
        colorShaderProgram.useProgram();
        colorShaderProgram.setUniform(modelViewProjectionMatrix, 1, 0, 0);
        mallet.bindData(colorShaderProgram);
        mallet.draw();

        // draw mallet 2
        positionObjectInScene(0, mallet.height / 2f, 0.4f);
        colorShaderProgram.setUniform(modelViewProjectionMatrix, 0, 0, 1);
        mallet.draw();

        // draw puck
        positionObjectInScene(0f, puck.height / 2f, 0);
        colorShaderProgram.setUniform(modelViewProjectionMatrix, .8f, 0.8f, 1);
        puck.bindData(colorShaderProgram);
        puck.draw();
    }

    public void positionTableInScene(){
        setIdentityM(modelMatrix, 0);
        rotateM(modelMatrix,0, -90, 1, 0, 0);
        multiplyMM(modelViewProjectionMatrix, 0, viewProjectionMatrix, 0, modelMatrix, 0);
    }

    public void positionObjectInScene(float x, float y, float z){
        setIdentityM(modelMatrix, 0);
        translateM(modelMatrix, 0, x, y, z);
        multiplyMM(modelViewProjectionMatrix, 0, viewProjectionMatrix, 0, modelMatrix, 0);
    }

    public void handlerTouchPress(float normalizedX, float normalizedY){
        Log.d(TAG, "handlerTouchPress: ");
    }

    public void handlerTouchDrag(float normalizedX, float normalizedY){
        Log.d(TAG, "handlerTouchDrag: ");
    }
}
